package com.ming.base.user.ui;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ming.base.authority.po.SysUserPO;
import com.ming.base.authority.service.IUserService;
import com.ming.core.exception.BaseException;

@Controller
@RequestMapping("/user")
public class UserAction {
	@Resource(name="userService")
	private IUserService userService;
@RequestMapping("/getUser")
@ResponseBody
public SysUserPO getUser(String id){
	SysUserPO user=null;
	try {
		user = userService.getUser("1");
	} catch (BaseException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return user;
}
//用户登录
@RequestMapping("/login")
public String login(SysUserPO user, HttpServletRequest request) {
	System.out.println(user.getLoginAccount());
	System.out.println(user.getLoginPass());
    Subject subject = SecurityUtils.getSubject();
    UsernamePasswordToken token = new UsernamePasswordToken(user.getLoginAccount(), user.getLoginPass());
    try{
        subject.login(token);//会跳到我们自定义的realm中
        request.getSession().setAttribute("user", user);
        return "/getUser";
    }catch(Exception e){
        e.printStackTrace();
        request.getSession().setAttribute("user", user);
        request.setAttribute("error", "用户名或密码错误！");
        return "/login";
    }
}

}
